let CryptoJS = require("crypto-js");
Date.prototype.format=function(format){
    var date={
        "M+": this.getMonth()+1, 
        "d+": this.getDate(), 
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth()+3)/3), 
        "S+": this.getMilliseconds(), 
    };
    if(/(y+)/i.test(format)){
        format=format.replace(RegExp.$1,(this.getFullYear()+'').substr(4-RegExp.$1.length));
    }
    for(var i in date){
        if(new RegExp("("+i+")").test(format)){
            format=format.replace(RegExp.$1, RegExp.$1.length==1?date[i]:("00"+date[i]).substr((""+date[i]).length));
        }
    }
    return format;
}
//格式转换
let formateData={
    //精确到天
    formateDate:function(str){
      if(str){
        // let reg=/(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2}).([0-9A-Za-z\-_\.]{4})/;
        // str=str.replace(reg,"$1-$2-$3");
        // str=(new Date(str)).toLocaleDateString();
        str=(new Date(str)).format('yyyy/MM/dd');
      }
        return str;
    },
    //精确到秒
    formateDateH:function(str){
      if(str){
        // let reg=/(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2}).([0-9A-Za-z\-_\.]{4})/;
        // str=str.replace(reg,"$1-$2-$3 $4:$5:$6");
        // str=(new Date(str)).toLocaleString();
        str=(new Date(str)).format('yyyy/MM/dd hh:mm:ss');
      }
        return str;
    },
    formateCertNo:function(str){
      if(str){
        let reg=/(\d{6})(\d{1,})(\d{3})(\d|\w)/;
        str=str.replace(reg,"$1***$3$4");
      }
      return str;
    },
    formatePhoneNumber:function(str){
      if(str){
        let reg=/(\d{3})(\d{1,})(\d{4})/;
        str=str.toString().replace(reg,"$1***$3");
      }
      return str;
    },
    formateEmail:function(str){
      if(str){
        let reg=/^([0-9A-Za-z\-_\.]{0,3})([0-9A-Za-z\-_\.]{0,})[@,@]([0-9a-zA-Z]+\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)$/g;
        str=str.replace(reg,"***$2@$3");
      }
      return str;
    },
    formateBankCarNo:function(str){
      if(str){
        let reg=/(\d{4})(\d{1,})(\d{4})/;
        str=str.toString().replace(reg,"$1***$3");
      }
      return str;
    },
    formateAddress:function(str){
      if(str){
        let reg=/(\d|[\uFF10-\uFF19])/g;
            str=str.replace(reg,"*");
      }
      return str;
    }
}
function request(queryStr){
    var api = {};
    api.queryString = (function () {
        var urlParams = {};
        var e,
        d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); },
        q = queryStr.substring(queryStr.indexOf('?') + 1),
        r = /([^&#=]+)=?([^&#]*)/g;
        while (e = r.exec(q)) urlParams[d(e[1])] = d(e[2]);
        return urlParams;
    })();
    api.getUrl = function () {
        var url = queryStr.substring(0, queryStr.indexOf('?') + 1);
        for (var p in api.queryString) { url += p + '=' + api.queryString[p] + "&"; }
        if (url.lastIndexOf('&') == url.length - 1) { return url.substring(0, url.lastIndexOf('&')); }
        return url;
    }
    return api;
}

let aes={
    aesEncrypt:function(message,key){
        var ciphertext = CryptoJS.AES.encrypt(message, CryptoJS.enc.Utf8.parse(key), {
            mode: CryptoJS.mode.ECB,
            padding:CryptoJS.pad.Pkcs7 
        });
        console.log("ciphertext.ciphertext："+ciphertext);
        return CryptoJS.enc.Base64.stringify(ciphertext.ciphertext);//密文 转 Base64字符串
    },
    aesDecrypt:function(crypted,key){
        var decrypted = CryptoJS.AES.decrypt({ciphertext: CryptoJS.enc.Base64.parse(crypted)},CryptoJS.enc.Utf8.parse(key),{
            mode: CryptoJS.mode.ECB,
            padding:CryptoJS.pad.Pkcs7 
        });
        return decrypted.toString(CryptoJS.enc.Utf8);
    }
}

module.exports = {
    extend: function(target, source, flag) {
        for(var key in source) {
            if(source.hasOwnProperty(key))
                flag ?
                    (target[key] = source[key]) :
                    (target[key] === void 0 && (target[key] = source[key]));
        }
        return target;
    },
    formateData:formateData,
    request:request,
    aes:aes
}